package com.arceus.leetcode;

/**
 * @author : iron
 * @version : 1.0.0
 * @date : 3:54 PM 2018/11/8
 */

public class T504 {

    public static String convertToBase7(int num) {
        if (num < 7 && num >= 0) {
            return String.valueOf(num);
        }
        boolean fushu = false;
        if (num < 0) {
            fushu = true;
            num = -num;
        }
        int shang = num / 7;
        int yushu = num % 7;
        int i = 1;
        while (shang >= 7) {
            yushu = (shang % 7) * (pow(10, i)) + yushu;
            shang = shang / 7;
            i++;
        }
        yushu = shang * (pow(10, i)) + yushu;
        if (fushu) {
            yushu = -yushu;
        }
        return String.valueOf(yushu);
    }

    public static void main(String[] args) {
        System.out.println(convertToBase7(56));

    }

    public static int pow(int m, int n) {
        int result = 1;        //0次方时为1
        for (int i = 0; i < n; i++) {
            result *= m;          //每次乘上次计算次方的结果
        }
        return result;            //计算好了，返回值
    }
}
